﻿using System;
using System.Collections.Generic;
using System.Text;
using SIOsystem.Model.ClassifyDataSetTableAdapters;
using System.Data;
using System.Windows.Forms;
using SIOsystem.Model.OperatorDataSetTableAdapters;

namespace SIOsystem.Control
{
    class DictionaryClass
    {
        static private DataTable getClass(long CID,string defaultString)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    DataTable dt = db.GetClassifyByPID(CID);
                    if (!defaultString.Equals(""))
                    {
                        DataRow dr = dt.NewRow();
                        dr["CID"] = -1;
                        dr["CName"] = defaultString;
                        dt.Rows.InsertAt(dr, 0);
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取分类出错", ex);
            }
        }

        static private DataTable getProject(string defaultString)
        {
            try
            {
                using (ProjectTableAdapter db = new ProjectTableAdapter())
                {
                    DataTable dt = db.GetProjectList();
                    DataRow dr = dt.NewRow();
                    if (!defaultString.Equals(""))
                    {
                        dr["PID"] = -1;
                        dr["ProjectName"] = defaultString;
                        dt.Rows.InsertAt(dr, 0);
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取项目出错", ex);
            }
        }
        static private DataTable getAllProject()
        {
            try
            {
                using (ProjectTableAdapter db = new ProjectTableAdapter())
                {
                    DataTable dt = db.GetAllProjectList();
                    DataRow dr = dt.NewRow();
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取项目出错", ex);
            }
        }

        static public bool fillMainClass(ref ComboBox mainList)//复用fillModelClass
        {
            fillModelClass(ref mainList, 0);
            return true;
        }

        static public bool fillProductClass(ref ListBox productList, long MID)
        {
            if (productList.Items.Count > 0)
            {
                productList.DataSource = null;
                productList.Items.Clear();
            }
            productList.DisplayMember = "CName";
            productList.ValueMember = "CID";
            productList.DataSource = getClass(MID, "");
            return true;
        }

        static public bool fillProductClass(ref CheckedListBox productList, long MID)
        {
            if (productList.Items.Count > 0)
            {
                productList.DataSource = null;
                productList.Items.Clear();
            }
            productList.ValueMember = "CID";
            productList.DataSource = getClass(MID, "");
            productList.DisplayMember = "CName";
            return true;
        }

        static public bool fillModelClass(ref ComboBox modelList, long PID)
        {
            if (modelList.Items.Count > 0)
            {
                modelList.DataSource = null;
                modelList.Items.Clear();
            }
            modelList.DisplayMember = "CName";
            modelList.ValueMember = "CID";
            modelList.DataSource = getClass(PID,"请选择...");
            if (modelList.Items.Count == 2)
            {
                modelList.SelectedIndex = 1;
            }
            return true;
        }

        static public bool fillModelClass(ref ComboBox modelList, long PID,string defaultString)
        {
            if (modelList.Items.Count > 0)
            {
                modelList.DataSource = null;
                modelList.Items.Clear();
            }
            modelList.DisplayMember = "CName";
            modelList.ValueMember = "CID";
            modelList.DataSource = getClass(PID, defaultString);
            if (modelList.Items.Count > 0)
            {
                modelList.SelectedIndex = 0;
            }
            return true;
        }

        static public bool fillDepartment(ref ComboBox projectList)
        {
            if (projectList.Items.Count > 0)
            {
                projectList.DataSource = null;
                projectList.Items.Clear();
            }
            projectList.DisplayMember = "ProjectName";
            projectList.ValueMember = "PID";
            projectList.DataSource = getProject("请选择...");
            if (projectList.Items.Count == 2)
            {
                projectList.SelectedIndex = 1;
            }
            return true;
        }

        static public bool fillDepartment(ref ListBox projectList)
        {
            if (projectList.Items.Count > 0)
            {
                projectList.DataSource = null;
                projectList.Items.Clear();
            }
            projectList.DisplayMember = "ProjectName";
            projectList.ValueMember = "PID";
            projectList.DataSource = getProject("");
            return true;
        }
        static public bool fillProject(ref ListBox projectList)
        {
            if (projectList.Items.Count > 0)
            {
                projectList.DataSource = null;
                projectList.Items.Clear();
            }
            projectList.DisplayMember = "ProjectName";
            projectList.ValueMember = "PID";
            projectList.DataSource = getAllProject();
            return true;
        }

        static public bool fillSetMainClass(ref ComboBox mainList)
        {
            if (mainList.Items.Count > 0)
            {
                mainList.DataSource = null;
                mainList.Items.Clear();
            }
            mainList.DisplayMember = "CName";
            mainList.ValueMember = "CID";
            mainList.DataSource = getClass(0, "未分类产品");
            return true;
        }

        static public bool fillOperator(ref DataGridView operatorList)
        {
            try
            {
                using (OperatorTableAdapter db = new OperatorTableAdapter())
                {
                    operatorList.DataSource = db.GetOperator();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取操作员出错", ex);
            }
        }

        static public bool fillRoles(ref ComboBox roleList)
        {
            try
            {
                using (OperatorTableAdapter db = new OperatorTableAdapter())
                {
                    roleList.DisplayMember = "RoleName";
                    roleList.ValueMember = "RID";
                    roleList.DataSource = db.GetRoles();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取角色出错", ex);
            }
        }

        static public string[] getProjectInfo(long pid)
        {
            try
            {
                using (ProjectTableAdapter db = new ProjectTableAdapter())
                {
                    DataTable dt = db.GetProjectById(pid);
                    if (dt.Rows.Count == 1)
                    {
                        return new string[] { dt.Rows[0]["ProjectID"].ToString()
                                            , dt.Rows[0]["ProjectName"].ToString()
                                            , dt.Rows[0]["ProjectPlace"].ToString()
                                            , dt.Rows[0]["SendAddr"].ToString()
                                            , dt.Rows[0]["Connecter"].ToString()
                                            , dt.Rows[0]["Payer"].ToString()
                                            , dt.Rows[0]["Status"].ToString() };
                    }
                    else
                    {
                        return new string[] { "", "", "", "", "", "", "" };
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
    }
}
